from hep.aida import IHistogram1D
from jhplot import H1D
from jhplot import HPlotJa
from jhplot.jadraw import JaKey
from jhplot.jadraw import JaText
from jhplot.jadraw import JaAxes
from jscinttools.utilities.aidautilities import AUtil
from jscinttools.utilities.aidautilities import AidaFileReader
from jscinttools.utilities.jhplotutilities import HPlotJaUtil
from java.awt import Color
from java.awt import Font
from java.io import File

#Locale.setDefault(Locale.ENGLISH)

""" Define if Showing or Saving the Plots """
showFigures = False
writeFigures = not showFigures


""" Define the Path and files names"""
aidaPath = "/home/marc-antoine/Documents/ZnWO4/Alpha"
aidaFile = ["Am241_Cs137_3400mK_501_PhotonReanalysis_NoTuples", "Am241_3400mK_501_PhotonReanalysis_NoTuples"]
specPath = "/Reanalysis/Ch2 Plus Ch1 Total Photons,All Cuts"
outPath = aidaPath
outFile = "ZnWO4_Spectra_Alpha_3K_Prelim_V2"

"""
###############################################################
# Define Graphical Attributs
###############################################################
"""
""" Define colors and fonts"""
figType = [".png", ".eps", ".pdf"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 20)
prelimFont = Font(Font.SERIF, Font.BOLD, 30)

"""
###############################################################
# Creating objects
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""
afrAG = AidaFileReader(aidaPath, aidaFile[0])
specAGHisto1D = afrAG.grabHisto1D(specPath)
AUtil.normHisto(specAGHisto1D, [250.0, 350.0])
specAG = H1D(specAGHisto1D)
specAG.setTitle(" ^{241}Am &amp; ^{137}Cs")
specAG.setStyle("h")
specAG.setColor(colors[0])
specAG.setPenWidth(2)
specAG.setErrAll(False)

afrA = AidaFileReader(aidaPath, aidaFile[1])
specAHisto1D = afrA.grabHisto1D(specPath)
AUtil.normHisto(specAHisto1D, [250.0, 350.0])
specA = H1D(specAHisto1D)
specA.setTitle(" ^{241}Am")
specA.setStyle("h")
specA.setColor(colors[1])
specA.setPenWidth(2)
specA.setErrAll(False)
#specA.setPenDash(3)

label = JaText("T = 3 K", 0.6, 0.25, "NDC")
label.setFont(legendFont)
label.setColor(colors[0])

#prelim = JaText("Preliminary", 0.6, 0.20, "NDC")
prelim = JaText("PRELIMINARY", 0.15, 0.40, "NDC")
prelim.setFont(prelimFont)
prelim.setColor(colors[1])
prelim.setRotAngle(-45)

enCs = JaText("&gamma;: 662 keV", 0.375, 0.47, "NDC")
enCs.setFont(statBoxFont)
enCs.setColor(colors[3])

enAm = JaText("&alpha;: 5.5 MeV ??", 0.575, 0.35, "NDC")
enAm.setFont(statBoxFont)
enAm.setColor(colors[3])

specAGKey = JaKey(20, specAG.getTitle(), 0.5, 0.20, "NDC")
specAGKey.setColor(specAG.getColor())
specAGKey.setKeyColor(specAG.getColor())
specAGKey.setPenWidth(2)
specAGKey.setFont(legendFont)

specAKey = JaKey(20, specA.getTitle(), 0.5, 0.25, "NDC")
specAKey.setColor(specA.getColor())
specAKey.setKeyColor(specA.getColor())
specAKey.setPenWidth(2)
specAKey.setFont(legendFont) 

"""
###############################################################
# Create the Plotter
###############################################################
"""
plotter = HPlotJa("Spectra", 600, 600, 1, 1, True)
plotter.setGTitle("Spectra in ZnWO_{4} at 3.4 K", titleFont)
plotter.setAntiAlias(False)
plotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
plotter.setRange(0, 400, 0, 1.5)
plotter.setLogScale(0, False)
plotter.setLogScale(1, False)
plotter.setTicFont(ticFont)
plotter.setShowStatBox(False)
plotter.showKey(False)
plotter.setNameX("Number of Photons")
plotter.setNameY("Normalized Counts")


""" Plots all the JHep objects"""
plotter.draw(specAG)
plotter.draw(specA)
plotter.draw(prelim)
plotter.draw(enCs)
plotter.draw(enAm)
plotter.draw(specAGKey)
plotter.draw(specAKey)



""" Modifies the JaAxes and the axes labels"""
theAxes = plotter.getPad()
theAxes.setLabelSpace(0, 0.0)
theAxes.setTicksSize(0, theAxes.getTicksSize(0))
theAxes.setSubTicksSize(0, theAxes.getSubTicksSize(0))
theAxes.setLabelSpace(1, 0.0)
#HPlotJaUtil.setTickLabelOneToPower(1, theAxes)

labX = plotter.getLabelX()[0][0]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, plotter)
labY = plotter.getLabelY()[0][0]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, plotter, -0.005)

plotter.update()

""" Show or save the plot"""
if showFigures:
	plotter.visible(True)
else:
	plotter.visible(False)
	for i in range(len(figType)):
		plotter.export(outPath + File.separator + outFile + figType[i])
		print("Writting File : " + outPath + File.separator + outFile + figType[i])
		
print("That's all folks !")
if not showFigures:
	exit()
